<?php

namespace app\models\merchant;

use app\models\constant\Status;
use app\models\Images;
use app\models\user\User;
use Yii;

/**
 * This is the model class for table "merchant".
 *
 * @property int $id
 * @property int $user_id
 * @property string $name
 * @property string $address
 * @property int $type
 * @property string $nickname
 * @property string $mobile
 * @property string $create_time
 * @property string $update_time
 * @property int $status 0 待审核 1 已通过, -1 驳回
 * @property string $bank_name
 * @property string $bank_number
 * @property string|null $reject
 * @property float $difference 让利百分比
 * @property int $image_id
 */
class Merchant extends \yii\db\ActiveRecord
{
    /**
     * status list
     * @var int[]
     */
    public static $statusMapping = [
        'ALL'       => -1,
        'OK'        => 1,
        'CLOSE'     => 0,
        'REJECT'    => 2,
        'CANCEL'    => 5,
        'DELETE'    => -99,
    ];

    /**
     * status display list
     * @var int[]
     */
    public static $statusMappingDisplay = [
        '全部'        => -1,
        '正常'        => 1,
        '待审核'       => 0,
        '驳回'        => 2,
        '已注销'        => 5,
        // '删除'       => -99,
    ];

    /**
     * Status list
     *
     * @return array
     */
    public static function statusList()
    {
        $list = [];
        foreach (self::$statusMappingDisplay as $key => $value) {
            $list[] = [
                'label' => $key,
                'value' => $value
            ];
        }

        return $list;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'merchant';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'type', 'status', 'image_id'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['reject'], 'string'],
            [['difference'], 'number'],
            [['name', 'address', 'nickname', 'mobile', 'bank_name', 'bank_number'], 'string', 'max' => 255],

            [['sort'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'name' => 'Name',
            'address' => 'Address',
            'type' => 'Type',
            'nickname' => 'Nickname',
            'mobile' => 'Mobile',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'status' => 'Status',
            'bank_name' => 'Bank Name',
            'bank_number' => 'Bank Number',
            'reject' => 'Reject',
            'difference' => 'Difference',
            'image_id' => 'Image ID',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getImage()
    {
        return $this->hasOne(Images::class, ['id' => 'image_id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }

    public function getType0()
    {
        return $this->hasOne(MerchantType::class, ['id' => 'type']);
    }

    public function getImages()
    {
        return $this->hasMany(MerchantImage::class, ['merchant_id' => 'id']);
    }
}
